//
//  PersonInfoViewController.m
//  Haliaeetus
//
//  Created by junli hou on 2018/4/30.
//  Copyright © 2018年 BMY. All rights reserved.
//

#import "PersonInfoViewController.h"

@interface PersonInfoViewController ()
@property (nonatomic,strong) UILabel *firstNameLabel;
@property (nonatomic,strong) UITextField *firstNameTextField;
@property (nonatomic,strong) UILabel *nameLabel;
@property (nonatomic,strong) UITextField *nameTextField;
@property (nonatomic,strong) UILabel *emailLabel;
@property (nonatomic,strong) UITextField *emailTextField;
@property (nonatomic,strong) UILabel *usPhoneLabel;
@property (nonatomic,strong) UITextField *usPhoneTextField;
@property (nonatomic,strong) UIButton *saveBtn;
@end

@implementation PersonInfoViewController


- (instancetype)initWithEditSuccessBlock:(EditPersonSuccessBlock)successBlock
{
    self = [super init];
    if (self) {
        _successBlock = successBlock;

    }
    return self;
}
- (void)loadView {
    [super loadView];
    self.title =@"个人信息";
    self.view.backgroundColor = [UIColor whiteColor];
    self.edgesForExtendedLayout = UIRectEdgeNone;
    [self.navigationController setLeftBtnWithTarget:self action:@selector(goBack)];
    [IQKeyboardManager sharedManager].enable = YES;
    [self initView];
    
}
- (void)goBack {
    [self.navigationController popViewControllerAnimated:YES];
    
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
}
- (void)initView{

    
    
    self.firstNameLabel = [[UILabel alloc]initWithFrame:CGRectMake(14,18,90,17)];
    self.firstNameLabel.text = @"姓";
    self.firstNameLabel.textColor = WS_RGBA(141,141,141,1);
    self.firstNameLabel.font = [UIFont systemFontOfSize:12];
    [self.view addSubview:self.firstNameLabel];
    
    self.firstNameTextField = [[UITextField alloc]initWithFrame:CGRectMake(14,CGRectGetMaxY(self.firstNameLabel.frame)+6,self.view.frame.size.width - 50,20)];

  
    self.firstNameTextField.backgroundColor = [UIColor whiteColor];
    self.firstNameTextField.placeholder=@"请填写您的姓";
    self.firstNameTextField.font = [UIFont systemFontOfSize:14];
    [self.view addSubview:self.firstNameTextField];
    
    UIImageView *firstNameLineView = [[UIImageView alloc]initWithFrame:CGRectMake(0,79.5,self.view.frame.size.width,0.5)];
    firstNameLineView.backgroundColor = WS_RGBA(234, 234, 234, 0.5);
    [self.view addSubview:firstNameLineView];
    
    self.nameLabel = [[UILabel alloc]initWithFrame:CGRectMake(14,CGRectGetMaxY(firstNameLineView.frame)+18,90,17)];
    self.nameLabel.text = @"名字";
    self.nameLabel.textColor = WS_RGBA(141,141,141,1);
    self.nameLabel.font = [UIFont systemFontOfSize:12];
    [self.view addSubview:self.nameLabel];
    
    self.nameTextField = [[UITextField alloc]initWithFrame:CGRectMake(14,CGRectGetMaxY(self.nameLabel.frame)+6,self.view.frame.size.width - 50,20)];
    
    self.nameTextField.backgroundColor = [UIColor whiteColor];
    self.nameTextField.placeholder=@"请填写您的名字";
    self.nameTextField.font = [UIFont systemFontOfSize:14];
    [self.view addSubview:self.nameTextField];
    
    UIImageView *nameLineView = [[UIImageView alloc]initWithFrame:CGRectMake(0,79.5+80,self.view.frame.size.width,0.5)];
    nameLineView.backgroundColor = WS_RGBA(234, 234, 234, 0.5);
    [self.view addSubview:nameLineView];
    
    self.emailLabel = [[UILabel alloc]initWithFrame:CGRectMake(14,CGRectGetMaxY(nameLineView.frame)+18,90,17)];
    self.emailLabel.text = @"邮箱";
    self.emailLabel.textColor = WS_RGBA(141,141,141,1);
    self.emailLabel.font = [UIFont systemFontOfSize:12];
    [self.view addSubview:self.emailLabel];
    
    self.emailTextField = [[UITextField alloc]initWithFrame:CGRectMake(14,CGRectGetMaxY(self.emailLabel.frame)+6,self.view.frame.size.width - 50,20)];
    
    self.emailTextField.backgroundColor = [UIColor whiteColor];
    self.emailTextField.placeholder=@"请填写您的电子邮箱";
    self.emailTextField.font = [UIFont systemFontOfSize:14];
    [self.view addSubview:self.emailTextField];
    
    UIImageView *emailLineView = [[UIImageView alloc]initWithFrame:CGRectMake(0,79.5+80+80,self.view.frame.size.width,0.5)];
    emailLineView.backgroundColor = WS_RGBA(234, 234, 234, 0.5);
    [self.view addSubview:emailLineView];
    
    
    
    self.usPhoneLabel = [[UILabel alloc]initWithFrame:CGRectMake(14,CGRectGetMaxY(emailLineView.frame)+18,90,17)];
    self.usPhoneLabel.text = @"北美电话";
    self.usPhoneLabel.textColor = WS_RGBA(141,141,141,1);
    self.usPhoneLabel.font = [UIFont systemFontOfSize:12];
    [self.view addSubview:self.usPhoneLabel];
    
    self.usPhoneTextField = [[UITextField alloc]initWithFrame:CGRectMake(14,CGRectGetMaxY(self.usPhoneLabel.frame)+6,self.view.frame.size.width - 50,20)];
    
    self.usPhoneTextField.backgroundColor = [UIColor whiteColor];
    self.usPhoneTextField.placeholder=@"请填写您的北美电话";
    self.usPhoneTextField.font = [UIFont systemFontOfSize:14];
    [self.view addSubview:self.usPhoneTextField];
    
    UIImageView *usPhoneLineView = [[UIImageView alloc]initWithFrame:CGRectMake(0,79.5+80+80+80,self.view.frame.size.width,0.5)];
    usPhoneLineView.backgroundColor = WS_RGBA(234, 234, 234, 0.5);
    [self.view addSubview:usPhoneLineView];
    
    
    


    
    //save
    self.saveBtn = [[UIButton alloc]init];
    self.saveBtn.frame =CGRectMake(15,CGRectGetMaxY(usPhoneLineView.frame) + 20,WS_SCREEN_FRAME_WIDTH-30,40);
    self.saveBtn.layer.cornerRadius=2;
    self.saveBtn.layer.masksToBounds = YES;
    self.saveBtn.backgroundColor = WS_RGB(255, 134, 35);
    self.saveBtn.titleLabel.font = [UIFont systemFontOfSize:16];
    [self.saveBtn setTitleColor:WS_RGB(255, 255, 255) forState:UIControlStateNormal];
    [self.saveBtn setTitle:@"保存" forState:UIControlStateNormal];
    [self.saveBtn addTarget:self action:@selector(VerificationText) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.saveBtn];
    
    
    if (self.userInfoDic) {
          self.firstNameTextField.text=WS_STR_NOT_NULL([self.userInfoDic objectForKey:@"first_name"]);
         self.nameTextField.text=WS_STR_NOT_NULL([self.userInfoDic objectForKey:@"last_name"]);
         self.emailTextField.text=WS_STR_NOT_NULL([self.userInfoDic objectForKey:@"email"]);
         self.usPhoneTextField.text=WS_STR_NOT_NULL([self.userInfoDic objectForKey:@"us_phone"]);

    }
}

-(void)savePersonInfo
{
    NSMutableDictionary *params = [[NSMutableDictionary alloc] init];
    
    [params setValue:self.firstNameTextField.text forKey:@"first_name"];
    [params setValue:self.nameTextField.text forKey:@"last_name"];
    [params setValue:self.emailTextField.text forKey:@"email"];
    [params setValue:self.usPhoneTextField.text forKey:@"us_phone"];
    
    
    [SVProgressHUD showWithStatus:@"loading"];
    [SVProgressHUD setDefaultMaskType:SVProgressHUDMaskTypeBlack];
    [WSNetwork WSHttpsByType:@"PUT" Url:[NSString stringWithFormat:@"%@%@",BMY_URL,@"/profile"] params:params onCompletion:^(NSMutableDictionary *dic){
        [SVProgressHUD dismiss];
        if ([[dic objectForKey:@"biz_action"] intValue]==0) {
            //获取成功
            WSToast(@"保存成功");

          [self performSelector:@selector(goBack) withObject:nil afterDelay:1.0];
        }else{
            //错误提示
            WSToast([dic objectForKey:@"biz_msg"]);
        }
        
    }onError:^(NSError *error){
         [SVProgressHUD dismiss];
        WSToast(@"保存失败")
    }];
    
    
    
}
- (void)VerificationText{
    
    
    if ([self.firstNameTextField.text isEqualToString:@""] || self.firstNameTextField.text.length == 0) {
        WSToast(@"请输入您的姓")
        
    }else if([self.nameTextField.text isEqualToString:@""] || self.nameTextField.text.length == 0){
        WSToast(@"请输入您名字")
        
    }else if([self.emailTextField.text isEqualToString:@""] || self.emailTextField.text.length == 0){
        WSToast(@"请输入您电子邮箱")
        
    }else{
        
        [self savePersonInfo];
    }
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
